package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.enums.UserType;
import com.weipai.springboot.model.Artisan;
import com.weipai.springboot.model.Shop;
import com.weipai.springboot.model.ShopCategory;
import com.weipai.springboot.model.ShopCollection;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.*;
import com.weipai.springboot.module.mapper.ShopCategoryMapper;
import com.weipai.springboot.module.mapper.ShopCollectionMapper;
import com.weipai.springboot.module.mapper.ShopMapper;
import com.weipai.springboot.module.service.ShopService;
import com.weipai.springboot.module.util.DateUtils;
import com.weipai.springboot.module.util.SessionUserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/07/27 17:50
 */
@Service
public class ShopServiceImpl implements ShopService {

    private static final Logger log = LoggerFactory.getLogger(ShopServiceImpl.class);

    @Autowired
    ShopDao shopDao;
    @Autowired
    ShopCategoryDao shopCategoryDao;
    @Autowired
    GoodsDao goodsDao;
    @Autowired
    ShopCollectionDao shopCollectionDao;
    @Autowired
    ShopCollectionMapper shopCollectionMapper;
    @Autowired
    SessionUserUtil sessionUserUtil;
    @Autowired
    ShopMapper shopMapper;
    @Autowired
    CustomerDao customerDao;

    
    //删除
    @Override
    public Response deleteById(String shopId) {
        Response response = new Response();
        Shop shop = shopDao.getShopById(shopId);
        if(null!=shop){
            shop.setDelTime(DateUtils.timeFormat(System.currentTimeMillis()));
            shop.setShopDel(-1);
            if(1==shopDao.updateByPrimaryKey(shop)){
                response.setCode(RetCode.SUCCESS);
            }else{
                response.setCode(RetCode.REQUEST_ERROR);
            }
        }else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //物理删除
    @Override
    public Response deleteByShopId(String shopId) {
        Response response = new Response();
            if(1==shopMapper.deleteByPrimaryKey(shopId)){
                response.setCode(RetCode.SUCCESS);
            }else{
                response.setCode(RetCode.REQUEST_ERROR);
            }
        return response;
    }

    //更改需求类别的状态
    @Override
    public Response changeStatus(String shopId) {
        Response response = new Response();
        Shop shop = shopDao.getShopById(shopId);
        if(null!=shop){
            shop.setUpdateTime(DateUtils.timeFormat(System.currentTimeMillis()));
            if(shop.getShopStatus()==0){
                shop.setShopStatus(-1);
            }else{
                shop.setShopStatus(0);
            }
            if(1==shopDao.updateByPrimaryKey(shop)){
                response.setCode(RetCode.SUCCESS);
            }else{
                response.setCode(RetCode.REQUEST_ERROR);
            }
        }else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    @Override
    public Response getShopById(String shopId) {

        Response response = new Response();
        Map<String, Object> shopMap = shopDao.getShopByIdAndMap(shopId);
        if(shopMap!=null){
            response.setCode(RetCode.SUCCESS);
            response.put("shop", shopMap);
        }else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //查询未删除/已删除的数据
    @Override
    public List<Shop> selectAllByShopDel(int del, String search, String sort, String way, Integer applicantStatus) {
        List<Shop> list = shopDao.selectAllByShopDel(del,search, sort, way, applicantStatus);
        for(Shop shop : list){
            ShopCategory cate = shopCategoryDao.getShopCategoryById(shop.getShopCategoryId());
            if(null!=cate){
                shop.setShopCategoryId(cate.getShopCategoryName());
            }
        }
        return list;
    }

    //批量删除
    @Override
    public Response batchDeleteById(String[] id) {
        Response response = new Response();
        if(shopDao.batchDeleteById(id)>0){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //店铺审核
    @Override
    @Transactional(readOnly=false, propagation = Propagation.REQUIRED)
    public Response audit(String id,String rejectionReason, String applicantStatus) {
        Response response = new Response();
        Shop shop = shopDao.getShopById(id);
        shop.setApplicantStatus(Integer.parseInt(applicantStatus)); // 审核的状态值
        shop.setRejectionReason(rejectionReason);// 被驳回理由
        if(1==shopDao.updateByPrimaryKey(shop)){
            if(1==customerDao.changeCustomerCategory(shop.getCustomerId(), String.valueOf(2))){
                response.setCode(RetCode.SUCCESS);
            }else{
                response.setCode(RetCode.REQUEST_ERROR);
            }
            
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //绑定手机号
    @Override
    public Response updateMobile(String shopId, String mobile, String cold, String oldCode) {
        Response response = new Response();
        if(!cold.equals(oldCode)){
            response.setCode(RetCode.COLD);
            return response;
        }
        if(shopDao.updateMobile(shopId,mobile)==1){
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //修改店铺邮箱
    @Override
    public Response updateEmail(String shopId, String email) {
        Response response = new Response();
        if(shopDao.updateEmail(shopId,email)==1){
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //前端商品详情页的店铺的关注人数、全部商品、店铺名称、logo信息
    @Override
    public Response getShopHeaderInfoByShopId(String shopId) {
        Response response = new Response();
        //查询店铺信息：关注人数、店铺名称、logo
        Map<String, Object> shopMap = shopDao.getShopHeaderInfoByShopId(shopId);
        if(null==shopMap){
            response.setCode(RetCode.NO_DATA);
            return response;
        }

        //查询此店铺下面一共有多少货品
        Integer amount = goodsDao.getGoodsAmountByShopId(shopId);
        //一个商铺有 多少人收藏
        int shopCollectionPeople = shopCollectionMapper.countByShopId(shopId);
        //判断此商店是否收藏
        String customerId = sessionUserUtil.getUserAttr().getCustomerId();

        List<ShopCollection> shopCollections = shopCollectionDao.verGoodsCollection(customerId,shopId);
        if(shopCollections.size()>0){
            shopMap.put("collectionShop", "已收藏");
        }else {
            shopMap.put("collectionShop", "未收藏");
        }
        shopMap.put("goodsAmount", amount);
        shopMap.put("shopCollectionPeople", shopCollectionPeople);
        response.setCode(RetCode.SUCCESS);
        response.setData(shopMap);
        return response;
    }

    //* 更新   店铺信息详情
    @Override
    public Response updateShopById(Shop shop) {
        Response response = new Response();
        Shop shop1 = shopDao.getShopById(shop.getShopId());
        if(shop1.getApplicantStatus()==-1){
            shop.setApplicantStatus(0);
        }
        if(shopDao.updateByPrimaryKeySelective(shop)==1){
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //更改客户的未结算值和已结算值
    @Override
    public Response changeWithdrawal(String shopId, String noWithdrawal, String haveWithdrawal) {
        Response response = new Response();
        if(shopDao.changeWithdrawal(shopId, noWithdrawal, haveWithdrawal)==1){
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }
}
